package service

import (
	"errors"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"
	"time"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/admin/service/dto"
	"tuowei.com/common/actions"
	cDto "tuowei.com/common/dto"
	"tuowei.com/pkg/xlog"
)

type SysIndexCategoryConfig struct {
	service.Service
}

// GetPage 获取SysIndexCategoryConfig列表
func (e *SysIndexCategoryConfig) GetPage(c *dto.SysIndexCategoryConfigGetPageReq, p *actions.DataPermission, list *[]models.SysIndexCategoryConfig, count *int64) error {
	var err error
	var data models.SysIndexCategoryConfig

	err = e.Orm.Debug().
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		xlog.Errorf("db error: %s", err)
		return err
	}
	return nil
}

// Insert 创建SysIndexCategoryConfig对象
func (e *SysIndexCategoryConfig) Insert(c *dto.SysIndexCategoryConfigInsertReq) error {
	var err error
	var data models.SysIndexCategoryConfig
	var i int64
	err = e.Orm.Model(&data).Where("name = ?", c.Name).Count(&i).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	if i > 0 {
		err := errors.New("院校已存在！")
		e.Log.Errorf("db error: %s", err)
		return err
	}
	c.Generate(&data)
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

func (e *SysIndexCategoryConfig) Update(c *dto.SysIndexCategoryConfigUpdateReq) error {
	var err error
	var model = models.SysIndexCategoryConfig{}
	var i int64
	err = e.Orm.Model(&model).Where("name = ?", c.Name).Where("id <> ?", c.Id).Count(&i).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	if i > 0 {
		err = errors.New("院校已存在！")
		e.Log.Errorf("db error: %s", err)
		return err
	}
	e.Orm.First(&model, c.GetId())
	c.Generate(&model)
	db := e.Orm.Save(&model)
	err = db.Error
	if err != nil {
		e.Log.Errorf("Service UpdateStatus error:%s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")

	}
	return nil
}

// Remove 删除SysClass
func (e *SysIndexCategoryConfig) Remove(c *dto.SysIndexCategoryConfigById, p *actions.DataPermission) error {
	var err error
	var data models.SysIndexCategoryConfig

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, c.GetId())
	if err = db.Error; err != nil {
		e.Log.Errorf("Error found in  RemoveSysUser : %s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

// GetPage 获取SysIndexCategoryConfig列表
func (e *SysIndexCategoryConfig) GetList(c *dto.SysIndexCategoryConfigSearchReq, list *[]models.SysIndexCategoryConfig) error {
	var err error
	err = e.Orm.Debug().
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
		).Table(new(models.SysIndexCategoryConfig).TableName()).Select("id,colleges_name").
		Find(list).Limit(-1).Offset(-1).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

func (e *SysIndexCategoryConfig) UpdateTopStatus(c *dto.SysIndexCategoryConfigUpdateTopStatusReq, p *actions.DataPermission) error {
	var err error
	data := make(map[string]interface{})
	data["updated_at"] = time.Now()
	data["is_show"] = c.IsShow
	data["updater"] = c.Updater
	data["update_by"] = c.UpdateBy
	db := e.Orm.Table(new(models.SysIndexCategoryConfig).TableName()).Where("id = ?", c.Id).UpdateColumns(data)
	if err = db.Error; err != nil {
		xlog.Errorf("更新失败：%v", err)
		return err
	}
	return nil
}

// Get 获取SysCourse对象
func (e *SysIndexCategoryConfig) Get(d *dto.SysIndexCategoryConfigById, p *actions.DataPermission, model *models.SysIndexCategoryConfig) error {
	var data models.SysIndexCategoryConfig

	err := e.Orm.Model(&data).Debug().
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error

	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		xlog.Errorf("db error: %s", err)
		return err
	}

	return nil
}
